// pages/apply/apply.js
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    backImg: app.globalData.iconImgUrl+"arrow-left.svg",
    pickerNameVisible: false,
    selectedItem: [],
    items: [],
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    backIcon: true,
    title: "发布认可",
    upImgVisible: true,
    imgArr: [],
    modalVisible: false,
    releaseMes: ''
  },
  showModal() {
    this.setData({
      modalVisible: true
    })
  },
  hideModal() {
    // var pages = getCurrentPages(); // 当前页面栈
    // // console.log(pages)
    // var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
    this.setData({
      modalVisible: false
    })
    // beforePage.getApList()
    wx.navigateBack()
  },
  deleteName(e) {
    // console.log(e.target)
    var index = e.target.dataset.index;
    var data = this.data.selectedItem;
    var pickData = this.data.items;
    pickData[index].checked = false;

    // delete data[index];
    data.splice(index, 1)
    console.log(data)
    this.setData({
      selectedItem: data,
      items: pickData
    })
  },
  nameChange() {
    const that = this
    this.setData({
      pickerNameVisible: true
    })

    for (var i = 0; i < that.data.items.length; i++) {
      var item = 'items[' + i + '].checked'
      that.setData({
        [item]: false
      })
      for (var j = 0; j < that.data.selectedItem.length; j++) {

        if (that.data.items[i].name == that.data.selectedItem[j].name) {
          that.setData({
            [item]: true
          })
        }
      }
    }
  },
  hidePickerName() {
    this.setData({
      pickerNameVisible: false
    })
  },
  checkboxChange(e) {
    var selectedArr = [],
      indexArr = [],
      that = this;
    this.data.items.forEach((P_item, index) => {
      var array = e.detail.value.forEach(item => {
        // console.log(P_item.name == item)
        if (P_item.name == item) {
          selectedArr.push(P_item);
          var item = 'items[' + index + '].checked';
          that.setData({
            [item]: true
          })
        }
      })
    })

    this.setData({
      selectedItem: selectedArr,
    })
  },
  textInput(e) {
    console.log(e.detail)
    this.setData({
      releaseMes: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},
  toApply() {
    // this.showModal()
    var porsonArr = [],
      that = this;
    this.data.selectedItem.forEach(item => {
      porsonArr.push(item.code)
    })

    // console.log(this.data.releaseMes)
    var data = {
      "content": this.data.releaseMes,
      "imgArr": this.data.imgArr,
      "personArr": porsonArr,
      "userCode": wx.getStorageSync('userCode')
    }
    // console.log('认可', data)
    wx.request({
      url: app.globalData.baseUrl + 'accept/approval.do',
      method: 'post',
      data: data,
      success(res) {
        // console.log(res)
        // console.log(res.data.rstype)
        if (res.data.rstype) {
          that.showModal()
        } else {
          wx.showToast({
            title: res.data.rsmsg,
            image: '/assets/images/icon/error.png'
          })
        }

      }
    })
  },
  takePhoto() {
    var that = this;

    wx.chooseImage({
      // count: 3, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function(res) {
        // success
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: app.globalData.baseUrl + 'application/fileUpload.do', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: "file",
          header: {
            "Content-Type": "multipart/form-data"
          },
          success: function(res) {
            var data = res.data
            var item1 = that.data.imgArr[0],
              item2 = that.data.imgArr[1],
              item3 = that.data.imgArr[2],
              imgArr = []
            // console.log(res, item2)
            if (item1 == null) {
              imgArr.push(app.globalData.serverUrl + res.data.split('"')[1])
              that.setData({
                imgArr: imgArr
              })
            }
            // console.log(item2, item2 == null)
            if (item1 != null && item2 == null) {
              that.setData({
                ['imgArr[1]']: app.globalData.serverUrl + res.data.split('"')[1]
              })
            }
            if (item1 != null && item2 != null && item3 == null) {
              that.setData({
                ['imgArr[2]']: app.globalData.serverUrl + res.data.split('"')[1],
                upImgVisible: false
              })
            }

            //do something
          },
          error(err) {
            console.log(err)
          }
        })
      },
      fail: function(errow) {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getEmployee()
  },
  // 获取选择人员名单
  getEmployee() {
    // console.log(app.globalData.userCode)
    const userCode = wx.getStorageSync("userCode"),
      that = this
    wx.request({
      url: app.globalData.baseUrl + 'employee/getUser.do',
      data: {
        userCode: userCode
      },
      method: 'post',
      success(res) {
        console.log('res', res)
        const nameArr = []
        res.data.userList.forEach(item => {
          nameArr.push({
            value: item.userName,
            name: item.userName,
            code: item.userCode
          })
        })
        that.setData({
          items: nameArr
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})